package com.usian.order.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.usian.jyzx.model.entity.order.OrderInfo;
import com.usian.jyzx.model.entity.product.ProductSku;
import com.usian.jyzx.model.entity.user.UserAddress;
import com.usian.jyzx.model.entity.user.UserInfo;
import com.usian.jyzx.model.vo.common.Result;
import com.usian.jyzx.model.vo.common.ResultCodeEnum;
import com.usian.jyzx.model.vo.h5.OrderItem;
import com.usian.jyzx.model.vo.h5.OrderItemInfo;
import com.usian.jyzx.model.vo.h5.TradeVo;
import com.usian.order.fegin.CartFegin;
import com.usian.order.fegin.ProductFegin;
import com.usian.order.fegin.UserFegin;
import com.usian.order.service.OrderInfoService;
import com.usian.order.service.OrderItemService;
import com.usian.order.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 07 15 14 01
 **/

@RestController
@RequestMapping("/api/order/orderInfo")
@Slf4j
public class OrderController {
    @Autowired
    CartFegin cartFegin;

    @Autowired
    ProductFegin productFegin;

    @Autowired
    UserFegin userFegin;

    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    OrderItemService orderItemService;

    @Autowired
    OrderService orderService;


    @GetMapping("/auth/trade")
    public  Result trade(HttpServletRequest request){
        log.info("order-service");
        String token = request.getHeader("token");
        return cartFegin.getAllOrderCkecked(token);
    }


    @PostMapping("/auth/submitOrder")
    public Result submitOrder(@RequestBody OrderItemInfo orderItemInfo,HttpServletRequest request){
        String token = request.getHeader("token");

       return  orderService.submitOrder(orderItemInfo,token);
    }

    @GetMapping("/auth/{orderId}")
    public  Result findOrderByorderId(@PathVariable("orderId") String orderId){
        OrderInfo orderInfo = orderInfoService.findOrderInfoByOrderId(orderId);
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/pay/{orderId}")
    public  OrderInfo findPayOrderByorderId(@PathVariable("orderId") String orderId){
        OrderInfo orderInfo = orderInfoService.findOrderInfoByOrderId(orderId);
        return orderInfo;
    }


    @GetMapping("auth/buy/{skuId}")
    public  Result buy(@PathVariable("skuId") Long skuId,HttpServletRequest request){
        //获取用户信息-->userId
        TradeVo token = orderService.buy(request.getHeader("token"), skuId);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }



    @GetMapping("/auth/{pageNum}/{pageSize}")
    public Result orderList(@PathVariable("pageNum") Integer pageNum,
                            @PathVariable("pageSize")Integer pageSize,
                            HttpServletRequest request,
                            Integer orderStatus){

        String token = request.getHeader("token");

        String str =(String) redisTemplate.opsForValue().get("token:" + token);

        UserInfo userInfo = JSON.parseObject(str, UserInfo.class);
        Integer userId = userInfo.getId();

        PageInfo<OrderInfo> pageInfo = orderInfoService.findOrderByUserIdAndOrderStatus(pageNum, pageSize, userId, orderStatus);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);

    }
}
